/*
 * CouponModel.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.Coupon;
import com.yidiandao.business.data.event.RefreshRedPacketEvent;
import com.yidiandao.business.data.net.BaseRequest;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.request.AddOfflineBonusRequest;
import com.yidiandao.business.data.request.ChooseCouponRequest;
import com.yidiandao.business.data.request.ChooseRedPacketRequest;
import com.yidiandao.business.data.request.QueryCouponRequest;
import com.yidiandao.business.data.request.QueryRedPacketRequest;
import com.yidiandao.business.data.response.AddOfflineBonusResponse;
import com.yidiandao.business.data.response.GetCartCouponListResponse;
import com.yidiandao.business.data.response.GetCartRedPacketListResponse;
import com.yidiandao.business.data.response.QueryCouponResponse;
import com.yidiandao.business.data.response.QueryRedPacketResponse;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.Functional;
import com.yidiandao.business.ui.widget.listview.displaylistitem.ChooseCouponListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.ChooseRedPacketListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.CouponDeprecatedListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.CouponValidListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.RedPacketUsedListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.RedPacketValidListItem;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 红包与优惠券的Model
 */
public class CouponModel implements ICouponModel {

    private NetBiz mNetBiz = new NetBiz();

    private List<DisplayListItem> mValidRedPaperDisplayListItems = new ArrayList<>();

    private List<DisplayListItem> mUsedRedPaperDisplayListItems = new ArrayList<>();

    private List<DisplayListItem> mDeprecatedRedPaperDisplayListItems = new ArrayList<>();

    private List<DisplayListItem> mValidCouponDisplayListItems = new ArrayList<>();

    private List<DisplayListItem> mUsedCouponDisplayListItems = new ArrayList<>();

    private List<DisplayListItem> mDeprecatedCouponDisplayListItems = new ArrayList<>();

    private int validRedPaperDataPager,
            deprecatedRedPaperDataPager,
            usedRedPaperDataPager,
            validCouponDataPager,
            usedCouponDataPager,
            deprecatedCouponDataPager;

    @Override
    public void loadValidRedPaperList(
            final boolean isRefreshing, String tag, final OnAccessNetListener listener) {

        // 如果下拉刷新，则 还有更多数据 page = 1
        if (isRefreshing) {
            validRedPaperDataPager = 1;
        }

        QueryRedPacketRequest request = new QueryRedPacketRequest();
        request.setUrl(Urls.RED_PAPER_LIST);
        request.setStatus(QueryRedPacketRequest.VALID);
        request.setTag(tag);
        request.setPage(validRedPaperDataPager);
        request.setPageSize(Common.PAGER_SIZE);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(
                request,
                QueryRedPacketResponse.class,
                new NetListener<QueryRedPacketResponse>() {
                    @Override
                    public void onSuccess(QueryRedPacketResponse response) {

                        // 页数增加
                        validRedPaperDataPager++;

                        if (isRefreshing) {
                            mValidRedPaperDisplayListItems.clear();
                        }

                        List<Coupon> bonusList = response.getData().getBonusList();

                        List<DisplayListItem> displayListItems = Functional.serverToClient(
                                bonusList,
                                new Functional.Processor<Coupon, DisplayListItem>() {
                                    @Override
                                    public DisplayListItem processToClient(Coupon coupon) {
                                        return new RedPacketValidListItem(coupon);
                                    }
                                });

                        mValidRedPaperDisplayListItems.addAll(displayListItems);

                        if (listener != null) {
                            listener.onSuccess(
                                    mValidRedPaperDisplayListItems,
                                    displayListItems.size() == 0,
                                    response.hasMoreData(), response.getData().getCount());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void loadDeprecatedRedPaperList(
            final boolean isRefreshing, String tag, final OnAccessNetListener listener) {

        // 如果下拉刷新，则 还有更多数据 page = 1
        if (isRefreshing) {
            deprecatedRedPaperDataPager = 1;
        }

        QueryRedPacketRequest request = new QueryRedPacketRequest();
        request.setUrl(Urls.RED_PAPER_LIST);
        request.setStatus(QueryRedPacketRequest.DEPRECATED);
        request.setTag(tag);
        request.setPage(deprecatedRedPaperDataPager);
        request.setPageSize(Common.PAGER_SIZE);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(
                request,
                QueryRedPacketResponse.class,
                new NetListener<QueryRedPacketResponse>() {
                    @Override
                    public void onSuccess(QueryRedPacketResponse response) {

                        // 页数增加
                        deprecatedRedPaperDataPager++;

                        if (isRefreshing) {
                            mDeprecatedRedPaperDisplayListItems.clear();
                        }

                        List<Coupon> bonusList = response.getData().getBonusList();

                        List<DisplayListItem> displayListItems = Functional.serverToClient(
                                bonusList,
                                new Functional.Processor<Coupon, DisplayListItem>() {
                                    @Override
                                    public DisplayListItem processToClient(Coupon coupon) {
                                        return new CouponDeprecatedListItem(coupon);
                                    }
                                });

                        mDeprecatedRedPaperDisplayListItems.addAll(displayListItems);

                        if (listener != null) {
                            listener.onSuccess(
                                    mDeprecatedRedPaperDisplayListItems,
                                    displayListItems.size() == 0,
                                    response.hasMoreData(), response.getData().getCount());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void loadUsedRedPaperList(final boolean isRefreshing, String tag,
                                     final OnAccessNetListener listener) {
        // 如果下拉刷新，则 还有更多数据 page = 1
        if (isRefreshing) {
            usedRedPaperDataPager = 1;
        }

        QueryRedPacketRequest request = new QueryRedPacketRequest();
        request.setUrl(Urls.RED_PAPER_LIST);
        request.setStatus(QueryRedPacketRequest.USED);
        request.setTag(tag);
        request.setPage(usedRedPaperDataPager);
        request.setPageSize(Common.PAGER_SIZE);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(
                request,
                QueryRedPacketResponse.class,
                new NetListener<QueryRedPacketResponse>() {
                    @Override
                    public void onSuccess(QueryRedPacketResponse response) {

                        // 页数增加
                        usedRedPaperDataPager++;

                        if (isRefreshing) {
                            mUsedRedPaperDisplayListItems.clear();
                        }

                        List<Coupon> bonusList = response.getData().getBonusList();

                        List<DisplayListItem> displayListItems = Functional.serverToClient(
                                bonusList,
                                new Functional.Processor<Coupon, DisplayListItem>() {
                                    @Override
                                    public DisplayListItem processToClient(Coupon coupon) {
                                        return new RedPacketUsedListItem(coupon);
                                    }
                                });

                        mUsedRedPaperDisplayListItems.addAll(displayListItems);

                        if (listener != null) {
                            listener.onSuccess(
                                    mUsedRedPaperDisplayListItems,
                                    displayListItems.size() == 0,
                                    response.hasMoreData(), response.getData().getCount());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void loadValidCouponList(
            final boolean isRefreshing, String tag, final OnAccessNetListener listener) {

        // 如果下拉刷新，则 还有更多数据 page = 1
        if (isRefreshing) {
            validCouponDataPager = 1;
        }

        QueryCouponRequest request = new QueryCouponRequest();
        request.setUrl(Urls.COUPON_LIST);
        request.setStatus(QueryCouponRequest.VALID);
        request.setTag(tag);
        request.setPage(validCouponDataPager);
        request.setPageSize(Common.PAGER_SIZE);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(
                request,
                QueryCouponResponse.class,
                new NetListener<QueryCouponResponse>() {
                    @Override
                    public void onSuccess(QueryCouponResponse response) {

                        // 页数增加
                        validCouponDataPager++;

                        if (isRefreshing) {
                            mValidCouponDisplayListItems.clear();
                        }

                        List<Coupon> bonusList = response.getData().getBonusList();

                        List<DisplayListItem> displayListItems = Functional.serverToClient(
                                bonusList,
                                new Functional.Processor<Coupon, DisplayListItem>() {
                                    @Override
                                    public DisplayListItem processToClient(Coupon coupon) {
                                        return new CouponValidListItem(coupon);
                                    }
                                });

                        mValidCouponDisplayListItems.addAll(displayListItems);

                        if (listener != null) {
                            listener.onSuccess(
                                    mValidCouponDisplayListItems,
                                    displayListItems.size() == 0,
                                    response.hasMoreData(), response.getData().getCount());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void loadUsedCouponList(final boolean isRefreshing, String tag,
                                   final OnAccessNetListener listener) {
        // 如果下拉刷新，则 还有更多数据 page = 1
        if (isRefreshing) {
            usedCouponDataPager = 1;
        }

        QueryRedPacketRequest request = new QueryRedPacketRequest();
        request.setUrl(Urls.COUPON_LIST);
        request.setStatus(QueryCouponRequest.USED);
        request.setTag(tag);
        request.setPage(usedCouponDataPager);
        request.setPageSize(Common.PAGER_SIZE);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(
                request,
                QueryRedPacketResponse.class,
                new NetListener<QueryRedPacketResponse>() {
                    @Override
                    public void onSuccess(QueryRedPacketResponse response) {

                        // 页数增加
                        usedCouponDataPager++;

                        if (isRefreshing) {
                            mUsedCouponDisplayListItems.clear();
                        }

                        List<Coupon> bonusList = response.getData().getBonusList();

                        List<DisplayListItem> displayListItems = Functional.serverToClient(
                                bonusList,
                                new Functional.Processor<Coupon, DisplayListItem>() {
                                    @Override
                                    public DisplayListItem processToClient(Coupon coupon) {
                                        return new RedPacketUsedListItem(coupon);
                                    }
                                });

                        mUsedCouponDisplayListItems.addAll(displayListItems);

                        if (listener != null) {
                            listener.onSuccess(
                                    mUsedCouponDisplayListItems,
                                    displayListItems.size() == 0,
                                    response.hasMoreData(), response.getData().getCount());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void loadDeprecatedCouponList(
            final boolean isRefreshing, String tag, final OnAccessNetListener listener) {

        // 如果下拉刷新，则 还有更多数据 page = 1
        if (isRefreshing) {
            deprecatedCouponDataPager = 1;
        }

        QueryCouponRequest request = new QueryCouponRequest();
        request.setUrl(Urls.COUPON_LIST);
        request.setStatus(QueryCouponRequest.DEPRECATED);
        request.setTag(tag);
        request.setPage(deprecatedCouponDataPager);
        request.setPageSize(Common.PAGER_SIZE);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(
                request,
                QueryCouponResponse.class,
                new NetListener<QueryCouponResponse>() {
                    @Override
                    public void onSuccess(QueryCouponResponse response) {

                        // 页数增加
                        deprecatedCouponDataPager++;

                        if (isRefreshing) {
                            mDeprecatedCouponDisplayListItems.clear();
                        }

                        List<Coupon> bonusList = response.getData().getBonusList();

                        List<DisplayListItem> displayListItems = Functional.serverToClient(
                                bonusList, new Functional.Processor<Coupon, DisplayListItem>() {
                                    @Override
                                    public DisplayListItem processToClient(Coupon coupon) {
                                        return new CouponDeprecatedListItem(coupon);
                                    }
                                });

                        mDeprecatedCouponDisplayListItems.addAll(displayListItems);

                        if (listener != null) {
                            listener.onSuccess(
                                    mDeprecatedCouponDisplayListItems, displayListItems.size() == 0,
                                    response.hasMoreData(), response.getData().getCount());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed();
                        }
                        return true;
                    }

                    @Override
                    public void onFinal() {
                    }
                });
    }

    @Override
    public void exchangedOfflineRedPacket(String redPacketCode, String tag,
                                          final OnExchangeOfflineRedPacketListener listener) {

        AddOfflineBonusRequest request = new AddOfflineBonusRequest();
        request.setUrl(Urls.EXCHANGE_OFFLINE_RED_PACKET);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setBonusCode(redPacketCode);

        mNetBiz.sendGetRequest(request, AddOfflineBonusResponse.class,
                new NetListener<AddOfflineBonusResponse>() {
                    @Override
                    public void onSuccess(AddOfflineBonusResponse response) {
                        if (listener != null) {
                            listener.onSuccess(response.getData().getBonusMoney());
                        }
                    }

                    @Override
                    public boolean onError(NetErrorInfo netErrorInfo) {

                        return true;
                    }

                    @Override
                    public boolean onFailed(BizErrorInfo bizErrorInfo) {
                        if (listener != null) {
                            listener.onFailed(bizErrorInfo.getMessage());
                        }
                        return false;
                    }

                    @Override
                    public void onFinal() {
                        if (listener != null) {
                            listener.onFinal();
                        }
                    }
                });

    }

    @Override
    public void postRefreshRedPacketEvent() {
        EventBus.getDefault().post(new RefreshRedPacketEvent());
    }

    @Override
    public void getCartRedPacketList(String pagerTag, final OnCartUsefulCouponListener listener) {
        BaseRequest request = new BaseRequest();
        request.setTag(pagerTag);
        request.setUrl(Urls.GET_CART_RED_PACKET_LIST);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(request, GetCartRedPacketListResponse.class, new NetListener<GetCartRedPacketListResponse>() {
            @Override
            public void onSuccess(GetCartRedPacketListResponse response) {
                List<Coupon> redPackets = response.getData().getRedPackets();
                Coupon choose = null;
                List<DisplayListItem> displayListItems = new ArrayList<>();
                for (Coupon redPacket : redPackets) {
                    if (redPacket.getIsSelected() == 1) {
                        choose = redPacket;
                    }
                    displayListItems.add(new ChooseRedPacketListItem(redPacket));
                }
                if (listener != null) {
                    listener.onSuccess(redPackets, displayListItems, choose);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void getCartCouponList(String pagerTag, final OnCartUsefulCouponListener listener) {
        BaseRequest request = new BaseRequest();
        request.setTag(pagerTag);
        request.setUrl(Urls.GET_CART_COUPON_LIST);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));

        mNetBiz.sendGetRequest(request, GetCartCouponListResponse.class, new NetListener<GetCartCouponListResponse>() {
            @Override
            public void onSuccess(GetCartCouponListResponse response) {
                List<Coupon> coupons = response.getData().getCoupons();
                Coupon choose = null;
                List<DisplayListItem> displayListItems = new ArrayList<>();
                for (Coupon coupon : coupons) {
                    if (coupon.getIsSelected() == 1) {
                        choose = coupon;
                    }
                    displayListItems.add(new ChooseCouponListItem(coupon));
                }
                if (listener != null) {
                    listener.onSuccess(coupons, displayListItems, choose);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void chooseRedPacket(String tag, String redPacketId, final OnAccessNetListener listener) {

        ChooseRedPacketRequest request = new ChooseRedPacketRequest();
        request.setUrl(Urls.CHOOSE_RED_PACKET);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setRedPacketId(redPacketId);

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess(null, false, false, null);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }

    @Override
    public void chooseCoupon(String tag, String couponId, final OnAccessNetListener listener) {
        ChooseCouponRequest request = new ChooseCouponRequest();
        request.setUrl(Urls.CHOOSE_COUPON);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setCouponId(couponId);

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess(null, false, false, null);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {

            }
        });
    }
}
